import http from "@utils/request";

interface Irole {
    current: number;
    size: number;
    roleName?: string;
    rolePerm?: string;
    enabled?: string;
}

export interface Role {
    id: string;
    roleName: string;
    rolePerm: string;
    unitId: string;
    dataPrivileges: number;
    enabled: number;
    createBy: string | null;
    createTime: number | null;
    updateBy: string | null;
    updateTime: number | null;
    descript: string | null;
}

export interface IroleApiResponse {
    code: string;
    message: string;
    data: {
        records: Role[];
        total: number;
        pageSize: number;
        page: number;
        orders: any[];
        optimizeCountSql: boolean;
        searchCount: boolean;
        countId: string | null;
        maxLimit: number | null;
        pages: number;
    };
}


//角色列表
export const rolePage = (data: Irole): Promise<IroleApiResponse> => {
    return http.get<IroleApiResponse>('/system_role/page', data)
}


export interface IRoleMenuItem {
    id: string;
    name: string;
    parentId: string;
    sort: number;
    path: string;
    query: null;
    component: string;
    cache: number;
    type: number;
    visible: number;
    enabled: number;
    perms: string;
    icon: string;
    remark: null;
    createTime: null;
    children?: IRoleMenuItem[]
}
interface IRoleMenu {
    code: string;
    message: string;
    data: {
        current: number
        pages: number
        size: number
        total: number
        records: IRoleMenuItem[]
    }
}
interface IRoleAllMenu {
    code: string;
    message: string;
    data: IRoleMenuItem[]
}

//菜单权限分页
export const menuTree = (data: {
    current: string;
    size: string;
    enabled: string;
}): Promise<IRoleMenu> => {
    return http.get<IRoleMenu>('system_menu/page', data);
}
//菜单树
export const menuAllTree = (): Promise<IRoleAllMenu> => {
    return http.get<IRoleAllMenu>('system_menu/tree');
}

interface IRoleAdd {
    id?: string | undefined;
    roleName: string;
    rolePerm: string;
    enabled: string;
    descript: string | undefined;
    permissionIds: string[];
}

interface IRoleAddData {
    code: number;
    message: string;
    data: null;
}

//添加角色
export const roleAdd = (data: IRoleAdd): Promise<IRoleAddData> => {
    return http.post<IRoleAddData>('/system_role/', data)
}

interface IRoleDel {
    code: number;
    message: string;
    data: null;
}

//删除角色
export const roleDelete = (data: string): Promise<IRoleDel> => {
    return http.delete<IRoleDel>(`/system_role/${data}`)
}

interface IRoleUpate {
    id: string
    roleName: string;
    rolePerm: string;
    unitId: string;
    dataPrivileges: string;
    enabled: string;
    createBy: null;
    createTime: number;
    updateBy: null;
    updateTime: null,
    descript: string | null;
    permissions: string[];
}
interface IUpate {
    message: string;
    code: string;
    data: IRoleUpate;
}
//角色详情
export const roleDetail = (data: string): Promise<IUpate> => {
    return http.get<IUpate>(`/system_role/${data}`)
}



//修改角色
export const roleUpdate = (data: IRoleAdd): Promise<IRoleDel> => {
    return http.put<IRoleDel>('/system_role/', data)
}


